﻿using System.Collections.Generic;
using System.Linq;
using Abp.Collections.Extensions;
using Abp.Runtime.Session;
using Abp.Timing.Timezone;
using LeadThing.AbpZeroTemplate.Authorization.Users.Dto;
using LeadThing.AbpZeroTemplate.DataExporting.Excel.EpPlus;
using LeadThing.AbpZeroTemplate.Dto;

namespace LeadThing.AbpZeroTemplate.Authorization.Users.Exporting
{
    public class UserListExcelExporter : EpPlusExcelExporterBase, IUserListExcelExporter
    {
        private readonly ITimeZoneConverter _timeZoneConverter;
        private readonly IAbpSession _abpSession;

        public UserListExcelExporter(
            ITimeZoneConverter timeZoneConverter, 
            IAbpSession abpSession)
        {
            _timeZoneConverter = timeZoneConverter;
            _abpSession = abpSession;
        }

        public FileDto ExportToFile(List<UserListDto> userListDtos)
        {
            return CreateExcelPackage(
                "UserList.xlsx",
                excelPackage =>
                {
                    var sheet = excelPackage.Workbook.Worksheets.Add(L("Users"));
                    sheet.OutLineApplyStyle = true;

                    AddHeader(
                        sheet,
                        L("Name"),
                        L("PhoneNumber"),
                        L("EmailAddress"),
                        L("TownID"),
                        L("AreaID"),
                        L("VillageID")
                        );

                    AddObjects(
                        sheet, 2, userListDtos,
                        _ => _.Name,
                        _ => _.PhoneNumber,
                        _ => _.EmailAddress,
                        _ => _.TownTitle,
                        _ => _.AreaTitle,
                        _ => _.VillageTitle
                        );

                    //Formatting cells

                    var lastLoginTimeColumn = sheet.Column(8);
                    lastLoginTimeColumn.Style.Numberformat.Format = "yyyy-mm-dd";

                    var creationTimeColumn = sheet.Column(10);
                    creationTimeColumn.Style.Numberformat.Format = "yyyy-mm-dd";

                    for (var i = 1; i <= 10; i++)
                    {
                        sheet.Column(i).AutoFit();
                    }
                });
        }

        public FileDto DownloadUsersTemplateExcel()
        {
            var file = new FileDto(@"网格员导入.xls", Net.MimeTypes.MimeTypeNames.ApplicationVndMsExcel);
            file.FileToken = @"网格员导入模板.xls";

            return file;
        }
    }
}
